﻿using System;
using System.ComponentModel;

namespace MvvmEx.Helpers
{
    /// <summary>
    /// 枚举辅助类
    /// </summary>
    public static class EnumHelper
    {
        public static string GetEnumDescription(Enum value)
        {
            if (value == null)
            {
                return string.Empty;
            }

            var description = value.ToString();

            var fieldInfo = value.GetType().GetField(description);

            var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attributes.Length > 0)
            {
                description = attributes[0].Description;
            }
            else
            {
                return string.Empty;
            }

            return description;
        }
    }
}